<template>
  <div class="home">
    <div class="page-header">
      <h1 class="title">微信场景实现系统</h1>
      <p class="subtitle">集成各种微信功能，快速构建微信生态应用</p>
    </div>

    <div class="container">
      <div class="grid">
        <div
          v-for="feature in allFeatures"
          :key="feature.id"
          class="card feature-card"
          @click="navigateToFeature(feature.route)"
        >
          <div class="feature-icon" :style="{ backgroundColor: feature.color }">
            {{ feature.icon }}
          </div>
          <h3 class="feature-title">{{ feature.title }}</h3>
          <p class="feature-description">{{ feature.description }}</p>
          <div class="feature-status">
            <span class="status-badge" :class="feature.status">
              {{ getStatusText(feature.status) }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="stats-section">
      <div class="container">
        <h2 class="section-title">功能统计</h2>
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-number">{{ allFeatures.length }}</div>
            <div class="stat-label">可用功能</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ paymentHistory.length }}</div>
            <div class="stat-label">支付记录</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ shareHistory.length }}</div>
            <div class="stat-label">分享记录</div>
          </div>
          <div class="stat-card">
            <div class="stat-number">{{ isLoggedIn ? 1 : 0 }}</div>
            <div class="stat-label">登录状态</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useWeChatStore } from '@/stores/wechat'
import { wechatApi } from '@/api/wechat'
import type { FunctionItem } from '@/types'

const router = useRouter()
const wechatStore = useWeChatStore()

// 从后端获取的功能列表
const functionList = ref<FunctionItem[]>([])
const loading = ref(false)

const activeFeatures = computed(() => wechatStore.activeFeatures)
const paymentHistory = computed(() => wechatStore.paymentHistory)
const shareHistory = computed(() => wechatStore.shareHistory)
const isLoggedIn = computed(() => wechatStore.isLoggedIn)

// 获取功能列表
const fetchFunctionList = async () => {
  try {
    loading.value = true
    const response = await wechatApi.getFunctionList()
    if (response.code === 200) {
      functionList.value = response.data
    }
  } catch (error) {
    console.error('获取功能列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 将后端功能列表转换为前端需要的格式
const backendFeatures = computed(() => {
  return functionList.value.map(func => ({
    id: func.id,
    title: func.name,
    description: func.description,
    icon: getIconForFunction(func.icon),
    route: func.path,
    color: getColorForFunction(func.id),
    status: func.enabled ? 'active' : 'inactive'
  }))
})

// 合并本地和后端功能
const allFeatures = computed(() => {
  return [...activeFeatures.value, ...backendFeatures.value]
})

const navigateToFeature = (route: string) => {
  router.push(route)
}

const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    active: '可用',
    inactive: '不可用',
    coming: '即将推出'
  }
  return statusMap[status] || '未知'
}

// 根据功能图标名称返回对应的emoji
const getIconForFunction = (iconName: string) => {
  const iconMap: Record<string, string> = {
    home: '🏠',
    login: '🔐',
    auth: '✅',
    location: '📍',
    payment: '💳',
    qrcode: '📱',
    share: '📤'
  }
  return iconMap[iconName] || '⚙️'
}

// 根据功能ID返回对应的颜色
const getColorForFunction = (id: string) => {
  const colorMap: Record<string, string> = {
    '1': '#6366f1', // 首页 - 蓝色
    '2': '#10b981', // 登录 - 绿色
    '3': '#f59e0b', // 认证 - 橙色
    '4': '#ef4444', // 位置 - 红色
    '5': '#8b5cf6', // 支付 - 紫色
    '6': '#06b6d4', // 二维码 - 青色
    '7': '#84cc16'  // 分享 - 青绿色
  }
  return colorMap[id] || '#6b7280'
}

onMounted(() => {
  fetchFunctionList()
})
</script>

<style scoped>
.home {
  min-height: 100vh;
  position: relative;
}

.page-header {
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  border-radius: 0 0 24px 24px;
  margin-bottom: 40px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
}

.title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 16px;
  text-align: center;
}

.subtitle {
  font-size: 1.1rem;
  color: #6b7280;
  max-width: 600px;
  margin: 0 auto;
  font-weight: 400;
  text-align: center;
}

.feature-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  text-align: center;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.feature-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  border-radius: 20px 20px 0 0;
}

.feature-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  margin: 0 auto 20px;
  color: white;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.feature-card:hover .feature-icon {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.feature-title {
  font-size: 1.6rem;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 16px;
  position: relative;
  z-index: 1;
}

.feature-description {
  color: #6b7280;
  line-height: 1.7;
  margin-bottom: 24px;
  font-size: 0.95rem;
  position: relative;
  z-index: 1;
}

.feature-status {
  display: flex;
  justify-content: center;
}

.status-badge {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 500;
}

.status-badge.active {
  background: #e8f5e8;
  color: #07c160;
}

.status-badge.inactive {
  background: #f5f5f5;
  color: #999;
}

.status-badge.coming {
  background: #fff3e0;
  color: #ff976a;
}

.stats-section {
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(10px);
  margin-top: 60px;
  padding: 60px 0;
  border-radius: 24px 24px 0 0;
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
}

.section-title {
  text-align: center;
  font-size: 2rem;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 40px;
  position: relative;
  z-index: 1;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 30px;
  max-width: 800px;
  margin: 0 auto;
}

.stat-card {
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 30px 20px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.12);
}

.stat-number {
  font-size: 2.5rem;
  font-weight: 700;
  color: #6366f1;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 1rem;
  color: #6b7280;
  font-weight: 500;
}

@media (max-width: 768px) {
  .title {
    font-size: 2rem;
  }
  
  .subtitle {
    font-size: 1rem;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }
  
  .stat-number {
    font-size: 2.5rem;
  }
}
</style>
